package org.abc.fund.controller.dataSource;

import lombok.RequiredArgsConstructor;
import org.abc.fund.service.dataSourceCollection.JoinQuantDataCollectionOrchestratorService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Controller for manually triggering JoinQuant data collection.
 */
@RestController
@CrossOrigin
@RequestMapping("/api/v1/joinquant")
@RequiredArgsConstructor
public class JQCollectionController {

    private final JoinQuantDataCollectionOrchestratorService orchestratorService;

    @PostMapping("/collect")
    public ResponseEntity<JoinQuantDataCollectionOrchestratorService.CollectionResult> triggerDataCollection(
            @RequestBody Map<String, Object> requestPayload) {

        String username = (String) requestPayload.get("username");
        String password = (String) requestPayload.get("password");
        @SuppressWarnings("unchecked")
        Map<String, Object> params = (Map<String, Object>) requestPayload.get("params");

        if (username == null || password == null || params == null) {
            return ResponseEntity.badRequest().body(JoinQuantDataCollectionOrchestratorService.CollectionResult.failure("Missing 'username', 'password', or 'params' in request body."));
        }

        JoinQuantDataCollectionOrchestratorService.CollectionResult result = orchestratorService.collectData(username, password, params);

        if (result.isSuccess()) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.status(500).body(result);
        }
    }
}




